package cn.com.pajx.pajx_visitor_v3.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rcw.swiperefreshrecyclerview.OnItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.adapter.KeyboardAdapter;
import cn.com.pajx.pajx_visitor_v3.base.BaseMvpActivity;
import cn.com.pajx.pajx_visitor_v3.bean.KeyboardBean;
import cn.com.pajx.pajx_visitor_v3.mvp.presenter.LoadDataPresenterImpl;
import cn.com.pajx.pajx_visitor_v3.utils.AppConstant;
import cn.com.pajx.pajx_visitor_v3.utils.LocalDataUtil;
import cn.com.pajx.pajx_visitor_v3.utils.LogUtils;
import cn.com.pajx.pajx_visitor_v3.utils.ToastUtil;

/**
 * 预约查询页面
 */
@SuppressLint("NonConstantResourceId")
public class QueryOrderActivity extends BaseMvpActivity<LoadDataPresenterImpl> {

    @BindView(R.id.view_phone)
    EditText viewPhone;
    @BindView(R.id.iv_animate_place)
    ImageView ivAnimatePlace;
    @BindView(R.id.iv_animate)
    ImageView ivAnimate;
    @BindView(R.id.rv_keyboard)
    RecyclerView rvKeyboard;

    private String key_word, school_name;
    private int school_id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_query_order;
    }

    @Override
    protected LoadDataPresenterImpl initPresenter() {
        return new LoadDataPresenterImpl();
    }

    @Override
    protected void initView() {
        setHeadTitle("预约查询");
        initDownTimer();
        //EditText事件监听
        initEditView();
        //初始化RecyclerView
        initRecyclerView();
        ivAnimate.setVisibility(View.VISIBLE);
        ivAnimatePlace.setVisibility(View.GONE);
        webpAnimate(ivAnimate, R.mipmap.order_query_animate);
    }

    /**
     * 隐藏系统软键盘
     */
    private void initEditView() {
        viewPhone.requestFocus();
        hideSoftKeyboard(viewPhone);
        viewPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                resetDownTimer();
            }

            @Override
            public void afterTextChanged(Editable editable) {
                key_word = editable.toString().trim();
            }
        });
    }

    private void hideSoftKeyboard(EditText viewInput) {
        viewInput.setShowSoftInputOnFocus(false);
        viewInput.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(viewInput.getWindowToken(), 0);

        });
    }

    /**
     * 初始化键盘的View
     */
    private void initRecyclerView() {
        if (rvKeyboard == null) return;
        List<KeyboardBean> keyboardBeans = LocalDataUtil.loadKeyboardBeans();
        rvKeyboard.setNestedScrollingEnabled(false);
        rvKeyboard.setLayoutManager(new GridLayoutManager(mContext, 3));
        KeyboardAdapter keyboardAdapter = new KeyboardAdapter(mContext, R.layout.keyboard_item, keyboardBeans);
        rvKeyboard.setAdapter(keyboardAdapter);
        keyboardAdapter.setOnItemClickListener(new OnItemClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void setOnClickListener(View view, int position) {
                resetDownTimer();
                keyboardAdapter.setSelection(position);
                KeyboardBean bean = keyboardBeans.get(position);
                switch (bean.getKey_code()) {
                    case "0"://数字
                        String oldValue = viewPhone.getText().toString().trim();
                        viewPhone.setText(oldValue + bean.getKey());
                        break;
                    case "1"://删除
                        String value = viewPhone.getText().toString().trim();
                        if (!TextUtils.isEmpty(value)) {
                            viewPhone.setText(value.substring(0, value.length() - 1));
                        }
                        break;
                    case "2"://查询
                        LogUtils.e("key_word=" + key_word);
                        if (TextUtils.isEmpty(key_word) || key_word.length() != 11) {
                            ToastUtil.toast("请输入正确的手机号码");
                            speckChinese("请输入正确的手机号码");
                            return;
                        }
                        Intent intent = new Intent(mContext, OrderActivity.class);
                        intent.putExtra(AppConstant.VISITE_PHONE,key_word);
                        startActivity(intent);
                        finish();
                        break;
                }
                viewPhone.setSelection(viewPhone.getText().length());//将光标移至文字末尾
            }

            @Override
            public void setOnLongClickListener(View view, int position) {

            }
        });
    }

    @OnClick(R.id.tv_clear)
    public void onClickViewed() {
        viewPhone.setText("");
    }

}